import { getRoleList, getDepartList, addRole, getAllRoleList, editRole, getRoleDesc, deleteRole, disableRole } from '@/api/innerProject/projectRoleManage'

const state = {
  tableData: [],
  departList: [],
  allRoleList: [],
  count: +''
}

const mutations = {
  // 设置更新列表
  update_list (state, tableData) {
    state.tableData = tableData
  },
  // 总数
  list_count (state, count) {
    state.count = count
  },
  update_departList (state, data) {
    state.departList = data
  },
  update_allRoleList (state, data) {
    state.allRoleList = data
  }
}

const actions = {
  // 刷新列表
  updateTable (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      getRoleList(payload).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_list', res.data.data.data)
          context.commit('list_count', res.data.data.count)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  updateDepartList (context) {
    return new Promise((resolve, reject) => {
      getDepartList().then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_departList', res.data.data)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  addRoleItem (context, payload) {
    payload.projectIds = [localStorage.getItem('projectId')]
    return new Promise((resolve, reject) => {
      addRole(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  updateAllRoleList (context) {
    return new Promise((resolve, reject) => {
      getAllRoleList().then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_allRoleList', res.data.data)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  editRoleData (context, payload) {
    return new Promise((resolve, reject) => {
      editRole(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  readRoleDetail (context, payload) {
    return new Promise((resolve, reject) => {
      getRoleDesc(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  deleteRoleItem (context, payload) {
    payload.projectId = localStorage.getItem('projectId')
    return new Promise((resolve, reject) => {
      deleteRole(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  disableRoleList (context, payload) {
    return new Promise((resolve, reject) => {
      disableRole(payload).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
